package com.ima.web.maintenance.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ima.web.beans.db.Categories;
import com.ima.web.beans.form.CategoriesForm;
import com.ima.web.service.CategoriesService;
import com.ima.web.service.UserService;

@Controller
@RequestMapping("/maintenance/categories")
public class CategoriesMaintenanceController {
	
	private CategoriesService categoriesService;
	private UserService userService;
	
	@Autowired
	public void setDependencies(CategoriesService categoriesService,
								UserService userService){
		this.categoriesService = categoriesService;
		this.userService = userService;
	}
	
	@RequestMapping(value="/edit/{id}")
	public String edit(ModelMap model,@PathVariable Integer id){
		boolean isEditing = false;
		System.out.println("categories maintenance id: "+id);
		
		CategoriesForm cf = new CategoriesForm();
		Categories c = categoriesService.getById(id);
		
		cf.setCategoriesId(c.getCategoriesId());
		cf.setDescription(c.getDescription());
		cf.setName(c.getName());
		
		System.out.println("categories id: "+cf.getCategoriesId());
		if(c.getApprover()!=null)
			cf.setApproved(true);
		
		model.put("category", cf);
		isEditing = true;
		
		model.put("isEditing",isEditing);
		
		return "admin/maintenance/categories/editCategories";
	}
	
	/**
	 * Default, makes it add
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/edit")
	public String edit(ModelMap model){
		model.put("category",new CategoriesForm());
		return "admin/maintenance/categories/editCategories"; 
	}
	
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(@ModelAttribute("category") CategoriesForm catForm,BindingResult result,ModelMap model){
		
//		Categories cat = new Categories();
//		cat.setName(catForm.getName());
//		cat.setDescription(catForm.getDescription());
//		
//		String username = SecurityContextHolder.getContext().getAuthentication().getName();
//		Users u = userService.getUserData(username);
//
//		if(catForm.getApproved()){
//			cat.setApprovedDate(new Date());
//			cat.setApprover(u);
//		}
//		
//		cat.setUpdatedDate(new Date());
//		
//		System.out.println("save category categories id: "+catForm.getCategoriesId());
//		if(catForm.getCategoriesId() == null)
//			cat.setCreatedDate(new Date());
//		
//		
//		categoriesService.insert(cat);
		System.out.println("categories id: "+catForm.getCategoriesId());
		categoriesService.insert(catForm);
		model.put("saved",true);
		model.put("msg", "successfully saved");
		return "admin/maintenance/categories/editCategories";
	}
	
	@RequestMapping(value="/view")
	public String list(ModelMap model){
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		List<Categories> approved = categoriesService.getApprovedBy(username);
		List<Categories> notYetApproved = categoriesService.getForProcessing();
		
		model.put("approved", approved);
		model.put("notYetApproved", notYetApproved);
		model.put("all",categoriesService.getList(new Categories()));
		
		return "admin/maintenance/categories/listCategories";
	}
	
	@RequestMapping(value="/view/{id}")
	public String view(ModelMap model,@PathVariable Integer id){
		System.out.println("categoriesView: "+id);
		model.put("category", 
					categoriesService.getById(id));
		
		return "admin/maintenance/categories/viewCategories";
	}
}
